package com.shqcjd.yunweiyanoperator.ui.activity.staff;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.pojo.CompanyBean;
import com.shqcjd.yunweiyanoperator.model.pojo.RepairRecordBean;
import com.shqcjd.yunweiyanoperator.model.pojo.VideotapeBean;
import com.shqcjd.yunweiyanoperator.ui.activity.preview.VideosActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.remote.RemoteFileListActivity;
import com.shqcjd.yunweiyanoperator.ui.activity.remote.VideotapeActivity;
import com.shqcjd.yunweiyanoperator.ui.adapter.RepairRecordAdapter;
import com.shqcjd.yunweiyanoperator.ui.adapter.VideotapeAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;
import com.shqcjd.yunweiyanoperator.util.WrapContentLinearLayoutManager;
import com.shqcjd.yunweiyanoperator.widget.AddTipDialog;
import com.shqcjd.yunweiyanoperator.widget.SendVideoDialog;
import com.shqcjd.yunweiyanoperator.widget.ShareDialog;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 录像列表
 */
public class VideotapeListActivity extends BaseActivity {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_include_add)
    TextView tvIncludeAdd;
    @BindView(R.id.rv_video_list)
    RecyclerView rvVideoList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private Context mContext;

    List<VideotapeBean> mList = new ArrayList<>();
    private VideotapeAdapter videotapeAdapter;
    private View notDataView;
    private final int REQUEST_CODE = 4396;
    private int friendId = -1;
    private String friendName = "";
    private String friendAppCode = "";
    private SendVideoDialog sendVideoDialog;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_videotape_list;
    }

    @Override
    public void initView() {
        mContext = this;
        tvTitle.setText("录像列表");
        tvIncludeAdd.setVisibility(View.VISIBLE);
        tvIncludeAdd.setTextColor(UIUtils.getColor(R.color.green));
        tvIncludeAdd.setText("新建录像");
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvVideoList.getParent(), false);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getDataList();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataList();
    }

    private void getDataList() {
        ApiService.videoRecordList()
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<List<VideotapeBean>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                     @Override
                    public void _onNext(ResponseData<List<VideotapeBean>> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        mList.clear();
                        mList.addAll(responseData.data);
                        if (videotapeAdapter == null) {
                            rvVideoList.setLayoutManager(new WrapContentLinearLayoutManager(mContext));
                            videotapeAdapter = new VideotapeAdapter(mList, mContext);
                            rvVideoList.setAdapter(videotapeAdapter);
                            videotapeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    if(view.getId() == R.id.rl_videotape){
                                        Intent it = new Intent(mContext, VideosActivity.class);
                                        it.putExtra("url", mList.get(position).videoUrl);
                                        it.putExtra("name", mList.get(position).title);
                                        startActivity(it);
                                    }else if(view.getId() == R.id.tv_videotape_send){
                                        sendVideoDialog = new SendVideoDialog(mContext);
                                        sendVideoDialog.setOnClickDialogListener(new SendVideoDialog.OnClickDialogListener() {
                                            @Override
                                            public void onClickSend(String info) {
                                                if(friendName.equals("")){
                                                    ToastUtils.showShort(mContext, "请选择好友");
                                                }else{
                                                    sendVideoHttp(info==null?"":info, mList.get(position).videoUrl);
                                                }
                                            }

                                            @Override
                                            public void onClickFrenid() {
                                                Intent it = new Intent(mContext, ContactsActivity.class);
                                                it.putExtra("type", 1);
                                                startActivityForResult(it, REQUEST_CODE);
                                            }
                                        });
                                        sendVideoDialog.show();
                                    }
                                }
                            });
                        }
                        if (mList.size() == 0) {
                            videotapeAdapter.setEmptyView(notDataView);
                        }
                        videotapeAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        swipeLayout.setRefreshing(false);
                    }
                });
    }


    private void sendVideoHttp(String info, String url){
        Map<String, String> map = new HashMap<>();
        map.put("appCode", friendAppCode);
        map.put("description", info);
        map.put("userId", friendId+"");
        map.put("userName", friendName);
        map.put("videoUrl", url);
        ApiService.sendVideo(new JSONObject(map).toString())
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在发送...");
                    }


                    @Override
                    public void _onNext(ResponseData responseData) {
                        if(responseData.retCode==10000){
                            sendVideoDialog.dismiss();
                            friendId = -1;
                            friendName = "";
                            friendAppCode = "";
                            ToastUtils.showShort(mContext, "发送成功");
                        }else{
                            ToastUtils.showShort(mContext, responseData.retMsg);
                        }

                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }

                });
    }

    @OnClick({R.id.btn_finish, R.id.tv_include_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                finish();
                break;
            case R.id.tv_include_add://新建录像
                startActivity(new Intent(mContext, VideotapeActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE == requestCode && RESULT_OK == resultCode) {
            friendId = data.getIntExtra("frenidId",-1);
            friendName = data.getStringExtra("frenidName");
            friendAppCode = data.getStringExtra("frenidAppCode");
            sendVideoDialog.setTvName(friendName);

        }
    }
}
